# -*- coding: utf-8 -*-
import random

from gemstoneScripts.config.itemList import *
from gemstoneScripts.config.enchantList import *

GEMSTONE_DICT = {
    ROUGH_MORGANITE: SMOOTH_MORGANITE,
    ROUGH_ROSE_QUARTZ: SMOOTH_ROSE_QUARTZ,
    ROUGH_SAPPHIRE: SMOOTH_SAPPHIRE,
    ROUGH_SPINEL: SMOOTH_SPINEL
}

GEMSTONE_LEVEL_DICT = {
    ROUGH_MORGANITE: 3,
    ROUGH_ROSE_QUARTZ: 3,
    ROUGH_SAPPHIRE: 3,
    ROUGH_SPINEL: 3
}

GEMSTONE_TOOLTIP = ['§g崭新出厂', '§a略有磨损', '§e久经沙场', '§c战痕累累', '§4崭新出土']

LEVEL_WEIGHT = [2, 4, 8, 16, 32]

GEMSTONE_ENCHANT = {
    SMOOTH_MORGANITE: [
        [(14, 3), (1, 4)],
        [(14, 2), (1, 3)],
        [(14, 1), (1, 2)]
    ],
    SMOOTH_ROSE_QUARTZ: [
        [(13, 2)],
        [(13, 2)],
        [(13, 1)]
    ],
    SMOOTH_SAPPHIRE: [
        [(16, 1)],
        [(16, 1)],
        []
    ],
    SMOOTH_SPINEL: [
        [(0, 4), (15, 5)],
        [(0, 3), (15, 3)],
        [(0, 2), (15, 1)]
    ]
}

GEMSTONE_MOD_ENCHANT = {
    SMOOTH_MORGANITE: [
        [(CHARGING, 3)],
        [(CHARGING, 2)],
        [(CHARGING, 1)]
    ],
    SMOOTH_ROSE_QUARTZ: [
        [(ROSE_THORN, 3), (MOLTEN_FIRE, 1)],
        [(ROSE_THORN, 2), (MOLTEN_FIRE, 1)],
        [(ROSE_THORN, 1)]
    ],
    SMOOTH_SAPPHIRE: [
        [(ICE_FROZEN, 3), (ICE_ARMOR, 3)],
        [(ICE_FROZEN, 2), (ICE_ARMOR, 2)],
        [(ICE_FROZEN, 1), (ICE_ARMOR, 1)]
    ],
    SMOOTH_SPINEL: [
        [(BETRAY, 3)],
        [(BETRAY, 2)],
        [(BETRAY, 1)]
    ]
}


def get_smooth_gemstone(rough_gemstone):
    smooth_gemstone_dict = {
        'newItemName': GEMSTONE_DICT[rough_gemstone],
        'newAuxValue': 0,
        'count': 1
    }
    level = GEMSTONE_LEVEL_DICT[rough_gemstone]
    level_sum = 0
    abrasion = -1
    for _ in xrange(level):
        level_sum += LEVEL_WEIGHT[_]
    rand = random.randint(0, level_sum - 1)
    for _ in xrange(level):
        rand -= LEVEL_WEIGHT[_]
        if rand < 0:
            abrasion = _
            break
    extra_id_dic = {'abrasion': abrasion}
    smooth_gemstone_dict['extraId'] = str(extra_id_dic)
    smooth_gemstone_dict['customTips'] = '%name%\n磨损度: ' + GEMSTONE_TOOLTIP[abrasion]
    return smooth_gemstone_dict
